﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.Mvc;
using Portal.Core.Adapters;
using Portal.Logic.Providers;
using Portal.Presentation.Providers;
using Portal.Presentation.Services;

namespace Portal.Presentation.Controllers
{
	[CustomHandleError(View = "Error")]
	public class SharedController : ControllerBase
	{
		#region Fields
		private readonly IUserAdapter _adapter; 
		#endregion

		#region .ctor
		/// <summary>
		/// Initializes a new instance of the <see cref="SharedController"/> class.
		/// </summary>
		/// <param name="security">The security.</param>
		/// <param name="session">The session.</param>
		/// <param name="adapter">The adapter.</param>
		/// <exception cref="System.ArgumentNullException">adapter</exception>
		public SharedController(ISecurityProvider security, ISessionService session, IUserAdapter adapter)
			: base(security, session)
		{
			if (adapter == null)
			{
				throw new ArgumentNullException("adapter");
			}
			_adapter = adapter;
		}
		#endregion

		[ChildActionOnly]
		public ActionResult Header()
		{
			var user = GetCurrentUser();
			if (user != null)
			{
				_adapter.Initialize(user);
				ViewBag.DisplayName = _adapter.DisplayName;
				return PartialView("_Header");
			}
			throw new SecurityException("User has not been found!");
		}
	}
}